package com.zhx.common.user.dao;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zhx.common.common.domain.enums.NormalOrNoEnum;
import com.zhx.common.common.domain.enums.YesOrNoEnum;
import com.zhx.common.common.domain.vo.req.CursorPageBaseReq;
import com.zhx.common.common.domain.vo.resp.CursorPageBaseResp;
import com.zhx.common.common.util.CursorUtils;
import com.zhx.common.user.domain.entity.User;
import com.zhx.common.user.domain.entity.UserBackpack;
import com.zhx.common.user.domain.enums.ChatActiveStatusEnum;
import com.zhx.common.user.mapper.UserMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author zhx
 * @since 2023-12-18
 */
@Service
public class UserDao extends ServiceImpl<UserMapper, User> {

    public User getByOpenId(String openId) {
//        return lambdaQuery()
//                .eq(User::getOpenId, openId)
//                .one();
        return this.getOne(new LambdaQueryWrapper<User>().eq(User::getOpenId, openId));

    }

    /**
     * 校验用户名是否存在
     *
     * @param name
     */
    public User getByName(String name) {
        return this.lambdaQuery().eq(User::getName, name).one();
    }


    /**
     * 修改用户名
     *
     * @param uid
     * @param name
     */
    public boolean modifyName(Long uid, String name) {
        return lambdaUpdate()
                .eq(User::getId, uid)
                .set(User::getName, name)
                .update();

    }


    /**
     * 修改用户状态
     *
     * @param id
     */
    public void invalidUid(Long id) {
        lambdaUpdate()
                .eq(User::getId, id)
                .set(User::getStatus, YesOrNoEnum.YES.getStatus())
                .update();
    }

    public List<User> getFriendList(List<Long> friendUids) {
        return lambdaQuery()
                .in(User::getId, friendUids)
                .select(User::getId, User::getActiveStatus, User::getName, User::getAvatar)
                .list();
    }

    public Integer getOnlineCount(List<Long> memberUidList) {
        return lambdaQuery()
                .eq(User::getActiveStatus, ChatActiveStatusEnum.ONLINE.getStatus())
                .in(CollectionUtil.isNotEmpty(memberUidList),User::getId,memberUidList)
                .count();
    }

    public CursorPageBaseResp<User> getCursorPage(List<Long> memberUidList, CursorPageBaseReq cursorPageBaseReq, ChatActiveStatusEnum online) {
        return CursorUtils.getCursorPageByMysql(this,cursorPageBaseReq,wrapper -> {
            wrapper.eq(User::getActiveStatus,online.getStatus());//筛选上线的
            wrapper.in(CollectionUtil.isNotEmpty(memberUidList),User::getId,memberUidList);
        },User::getLastOptTime);
    }

    public List<User> getMemberList() {
        return lambdaQuery()
                .eq(User::getStatus, NormalOrNoEnum.NORMAL.getStatus())
                .orderByDesc(User::getLastOptTime)
                .last("limit 100")
                .select(User::getId,User::getName,User::getAvatar)
                .list();
    }

    public void wearingBadge(Long uid, Long itemId) {

    }
}
